import api from './api/api.js'
import utils from '../utils/util.js'
import constant from '../constant/constant.js'
const cache = {
	setToken: function(token) {
		uni.setStorageSync(constant.StorageName.APP_LOGIN_TOKEN, token)
		if (!utils.isNullOrEmpty(token)) {
			this.updateUserInfo()
		}
	},
	getPlatformType() {
		return uni.getStorageSync("platformType")
	},
	getToken() {
		return uni.getStorageSync(constant.StorageName.APP_LOGIN_TOKEN)
	},
	isLogin: function() {
		return !utils.isNullOrEmpty(uni.getStorageSync(constant.StorageName.APP_LOGIN_TOKEN)) ? true : false
	},
	isLoginOnclick: function() {
		if (this.isLogin()) {
			return true
		} else {
			uni.navigateTo({
				url: '/pages/loginPage/login/login'
			});
			return false
		}
	},
	webURL: function() {
		return "https://www.thorui.cn/wx"
	},
	updateUserInfo: function() {
		let that = this;
		if (utils.isNullOrEmpty(this.getToken())) {
			uni.$emit(constant.EventKey.UPDATE_USER_INFO, {
				msg: '用户信息更新'
			})
			return
		}
		// api.getUserInfo().then((res) => {
		// 	var user = JSON.stringify(res.data.data);
		// 	uni.setStorageSync(constant.StorageName.APP_USER_INFO, user)
		// 	uni.$emit(constant.EventKey.UPDATE_USER_INFO, {
		// 		msg: '用户信息更新'
		// 	})
		// 	if (res.data.code == 200) {
		// 		if (res.data.data.status == -1 || res.data.data.status == 1) {
		// 			uni.showToast({
		// 				title: res.data.data.status == -1 ? '用户已被禁用' : '用户已被删除',
		// 				icon: 'none',
		// 				duration: 1000
		// 			})
		// 			setTimeout(function() {
		// 				var type = 2
		// 				api.logout(JSON.stringify(type)).then(res => {})
		// 				uni.removeStorageSync(constant.StorageName.APP_LOGIN_TOKEN)
		// 				uni.removeStorageSync(constant.StorageName.APP_USER_INFO)
		// 				uni.removeStorageSync("isFirst")
		// 				that.updateUserInfo()
		// 			})
		// 		}
		// 	}
		// }).catch((err) => {
		// 	console.log(err)
		// })
		// api.getRealNameAuthenticationDetail().then(res => {
		// 	if (res.data.code == 200) {
		// 		var isReal = res.data.data ? res.data.data.result == 2 ? 2 : 1 : 1
		// 		uni.setStorageSync("authentication-storage", isReal)
		// 	}
		// })
	},
	appUser() {
		if (utils.isNullOrEmpty(uni.getStorageSync(constant.StorageName.APP_USER_INFO))) {
			return null
		}
		return JSON.parse(uni.getStorageSync(constant.StorageName.APP_USER_INFO))
	},

	isReal() {
		if (utils.isNullOrEmpty(uni.getStorageSync("authentication-storage"))) {
			return 1
		}
		return JSON.parse(uni.getStorageSync("authentication-storage"))

	},
	clearIntervalTime(timer) {
		clearInterval(timer)
	},

	formatTime: function(d) {
		var t = d < 10 ? '0' + d : d;
		return t
	},

	toLogin() {
		uni.showModal({
			title: '温馨提示',
			content: '登录后可享受更多服务，是否立即登录',
			confirmText: '登录',
			success(res) {
				if (res.confirm) {
					uni.navigateTo({
						url: "/pages/loginPage/login/login"
					})
				}
			}
		})
	},
	switchOrderStatus: function(status, refundStatus, refundType) {
		let statusName = ''
		status = parseInt(status)
		refundStatus = refundStatus ? parseInt(refundStatus) : ''
		refundType = parseInt(refundType)
		if(refundStatus && ((refundType == 1 && refundStatus != 3) || (refundType == 2 && refundStatus != 5))){
			switch (refundType) {
				case 1:
					switch (refundStatus) {
						case 1:
							statusName = "申请退款"
							break;
						case 2:
							statusName = "商家同意退款"
							break;
					}
				case 2:
					switch (refundStatus) {
						case 1:
							statusName = "申请退款退货"
							break;
						case 2:
							statusName = "商家同意退款退货"
							break;
						case 3:
							statusName = "卖家待收货"
							break
						case 4:
							statusName = "交易完成"
							break
					}
			}
		}else{
			switch (status) {
				case 1:
					statusName = '待付款';
					break;
				case 2:
					statusName = '商家待发货';
					break;
				case 3:
					statusName = '卖家已发货';
					break;
				case 4:
					statusName = '已收货';
					break;
				case 5:
					statusName = '交易成功';
					break;
				case 7:
					statusName = '退款成功'
					break;
				case 6:
					statusName = '订单已取消'
					break
			}
		}
		return statusName;
	},

	switchStoreOrderStatus: function(status, refundStatus, refundType) {
		let statusName = ''
		status = parseInt(status)
		refundStatus = refundStatus ? parseInt(refundStatus) : ''
		refundType = refundType ? parseInt(refundType) : ''
		if(refundStatus && refundStatus != -1 && ((refundType == 1 && refundStatus != 3) || (refundType == 2 && refundStatus != 5))){
			switch (refundType) {
				case 1:
					switch (refundStatus) {
						case 1:
							statusName = "申请退款"
							break;
						case 2:
							statusName = "同意退款"
							break;
					}
				case 2:
					switch (refundStatus) {
						case 1:
							statusName = "申请退款退货"
							break;
						case 2:
							statusName = "同意退款"
							break;
						case 3:
							statusName = "用户发货"
							break
						case 4:
							statusName = "确认收货"
							break
					}
			}
		}else{
			switch (status) {
				case 1:
					statusName = '待付款';
					break;
				case 2:
					statusName = '用户已付款';
					break;
				case 3:
					statusName = '买家待收货';
					break;
				case 4:
					statusName = '买家已收货';
					break;
				case 5:
					statusName = '交易完成';
					break;
				case 7:
					statusName = '退款成功'
					break;
				case 6:
					statusName = '订单已取消'
					break
			}
		}
		return statusName;
	},

	changeTimeFormat(time) {
		time = time.replace(/-/g, '/')
		return time ? time.split(" ")[0].split("/").join('.') : ''
	},

	changeToTimeFormat(time) {
		time = time.replace(/-/g, '/')
		var y = new Date(time).getFullYear()
		var m = this.formatTime(new Date(time).getMonth() + 1)
		var d = this.formatTime(new Date(time).getDate())
		return y + '-' + m + '-' + d
	},
	NoToChinese(num) {
		if (!/^\d*(\.\d*)?$/.test(num)) {
			alert("Number is wrong!");
			return "Number is wrong!";
		}
		var AA = new Array("零", "一", "二", "三", "四", "五", "六", "七", "八", "九");
		var BB = new Array("", "十", "百", "千", "万", "亿", "点", "");
		var a = ("" + num).replace(/(^0*)/g, "").split("."),
			k = 0,
			re = "";
		for (var i = a[0].length - 1; i >= 0; i--) {
			switch (k) {
				case 0:
					re = BB[7] + re;
					break;
				case 4:
					if (!new RegExp("0{4}\\d{" + (a[0].length - i - 1) + "}$").test(a[0]))
						re = BB[4] + re;
					break;
				case 8:
					re = BB[5] + re;
					BB[7] = BB[5];
					k = 0;
					break;
			}
			if (k % 4 == 2 && a[0].charAt(i + 2) != 0 && a[0].charAt(i + 1) == 0) re = AA[0] + re;
			if (a[0].charAt(i) != 0) re = AA[a[0].charAt(i)] + BB[k % 4] + re;
			k++;
		}
		if (a.length > 1) //加上小数部分(如果有小数部分) 
		{
			re += BB[6];
			for (var i = 0; i < a[1].length; i++) re += AA[a[1].charAt(i)];
		}
		return re;
	}
}



module.exports = {
	getToken: cache.getToken,
	setToken: cache.setToken,
	isLogin: cache.isLogin,
	webURL: cache.webURL,
	updateUserInfo: cache.updateUserInfo,
	appUser: cache.appUser,
	isLoginOnclick: cache.isLoginOnclick,
	toLogin: cache.toLogin,
	clearIntervalTime: cache.clearIntervalTime,
	formatTime: cache.formatTime,
	switchOrderStatus: cache.switchOrderStatus,
	changeTimeFormat: cache.changeTimeFormat,
	changeToTimeFormat: cache.changeToTimeFormat,
	NoToChinese: cache.NoToChinese,
	getPlatformType: cache.getPlatformType,
	switchStoreOrderStatus: cache.switchStoreOrderStatus,
	isReal: cache.isReal
}
